<?php 
class CCommentController extends CController{
	protected $m_arrobjUsers;
	protected $m_objUser;
	protected $m_objComment;
	
	function __construct() {
		parent::__construct();
		return true;
	}
	
	function create() {
		parent::create();
	}
	
	function initialize() {
		parent::initialize();
	}
	
	function execute() {		
		
		switch( $this->m_strAction ) {
			
			case NULL:			
			case 'view_comments':
				$this->handleViewComments();
				break;
				
			case 'add_edit_comment':
				$this->handleAddEditComment();
				break;
							
			case 'insert_update_comment':
					$this->handleInsertUpdateComment();
				break;
			
			case 'update_comment_action':
				$this->handleUpdateCommentAction();
				break;
			
			default:
				// Empty default case
				
		}
	}

	/*************************************
	********** HANDLE FUNCTIONS **********
	*************************************/
		
	function handleViewComments() {
		if( true == isset( $_REQUEST['id'] ) ) {
			$this->m_arrobjComments = CComments::fetchCommentsByBlogId($_REQUEST['id'], $this->m_objDatabase );
		}	
		$this->displayComments();
	}
	
	function handleAddEditComment() {
	
	
		$this->m_objComment = new CComment();
		
		if( true == isset( $_REQUEST['id'] ) ) {
			$this->m_objComment = CComments::fetchCommentById( $_REQUEST['id'], $this->m_objDatabase );
		}
	
		if( true == isset( $_REQUEST['blog_id'] ) ) {
			$this->m_objComment->setBlogId( $_REQUEST['blog_id'] );
		}
		
		$this->displayAddEditComment();
	}
	
	
	function handleInsertUpdateComment() {
		
		switch( true ) {
			default:
				$arrmixFieldsValues = $this->m_objData->getPostData( array( 'comment' ) );
		
				$this->m_objComment = CComments::setValues( 'CComment', $arrmixFieldsValues );
				$this->m_objComment->setCreatedBy($_SESSION['udeal.in']['login_user_id']);
				$this->m_objComment->setCreatedOn(date('Y-m-d'));
				/* if( false == $this->m_objComment->validate( $arrmixFieldsValues, array_keys( $arrmixFieldsValues ) ) ) {
					break;
				} */
				if( false == is_null( $this->m_objComment->getId() ) && '' != trim( $this->m_objComment->getId() ) ) {
				
					if( false == $this->m_objComment->update( $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setErrorMessage( 'Failed to update comment.' );
					} else {
						$this->m_objErrorMessage->setSuccessMessage( 'comment update successfully. ' );
					}
				} else {
					
					if( false == $this->m_objComment->insert( $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setErrorMessage( 'Failed to create comment.' );
					} else {
						$this->m_objErrorMessage->setSuccessMessage( 'comment created successfully. ' );
					}
				}
				redirect( '/admin/blog/view_blog_in_detail/id/'. $this->m_objComment->getBlogId() );
		}
 		redirect( '/admin/comment/add_edit_comment' );
	}

	function handleUpdateCommentAction() {
	
		switch( true ) {
			default:
	
				$strAction = $this->m_objData->getPostData( array( 'comment', 'action' ) );
				if( false == isset( $strAction  ) ) {
					$this->m_objErrorMessage->setErrorMessage( 'Please select action ' );
					break;
				}
	
				$arrintCommentIds = $this->m_objData->getPostData( array( 'comment', 'id' ) );
				if( false == valArr( $arrintCommentIds ) ) {
					$this->m_objErrorMessage->setErrorMessage( 'Please select records to delete ' );
					break;
				}
	
				$this->m_objComment = new CComment();
				if( false == valObj( $this->m_objComment, 'CComment' ) ) {
					$this->m_objErrorMessage->setErrorMessage( 'Failed to create object' );
					break;
				}
	
				if( 'delete' == $strAction ) {
					if( true == $this->m_objComment->deleteCommentsByIds( $arrintCommentIds, $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setSuccessMessage( 'Record(s) deleted successfully.' );
					}
				} else {
					if( true == $this->m_objComment->updateCommentsActionByIds( $strAction, $arrintCommentIds, $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setSuccessMessage( 'Record(s) updated successfully.' );
					}
				}
					
		}
		redirect( '/admin/comment' );
	}
	
	
	function handleViewUser() {
		$intUserId = $_REQUEST['id'];
		$this->m_objUser = CUsers::fetchUserById( $intUserId, $this->m_objDatabase );
	
		$this->displayViewUser();
	}
	
	/**************************************
	********** DISPLAY FUNCTIONS **********
	**************************************/		
	
	function displayAddEditComment() {
		$this->display();
	
		$this->m_objSmarty->assign_by_ref( 'fckeditor', $this->m_objFCKeditor );
		$this->m_objSmarty->assign_by_ref( 'comment', $this->m_objComment );
		$this->m_objSmarty->display( TEMPLATE_DIR_PATH . '/admin/comment/view_add_edit_comment.tpl' );
	}
	
	function displayComments() {
		$this->display();

		$this->m_objSmarty->assign( 'comments', $this->m_arrobjComments );
		$this->m_objSmarty->display( TEMPLATE_DIR_PATH . '/admin/comment/view_comments.tpl');

	}

}
?>